import axios from 'axios';
import { BASE_URL } from '../config';
import { mockCourses } from './mockData';

const axiosInstance = axios.create({
  baseURL: BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 模拟API调用延迟
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// 获取所有可用分类
const getCategories = () => {
  const categories = [...new Set(mockCourses.map(course => course.category))];
  return categories;
};

export const courseApi = {
  // 获取课程列表
  getCourses: async () => {
    const response = await axiosInstance.get('/courses');
    return response.data;
  },

  // 获取所有分类
  getCategories: async () => {
    await delay(300);
    return getCategories();
  },

  // 获取课程详情
  getCourseById: async (courseId) => {
    const response = await axiosInstance.get(`/courses/${courseId}`);
    return response.data;
  },

  // 创建课程
  createCourse: async (courseData) => {
    const response = await axiosInstance.post('/courses', courseData);
    return response.data;
  },

  // 更新课程
  updateCourse: async (courseId, courseData) => {
    const response = await axiosInstance.put(`/courses/${courseId}`, courseData);
    return response.data;
  },

  // 删除课程
  deleteCourse: async (courseId) => {
    const response = await axiosInstance.delete(`/courses/${courseId}`);
    return response.data;
  }
}; 